package cn.iclass.vodfileupload.controller;

import cn.hutool.core.io.FileUtil;
import cn.iclass.vodfileupload.UploadVideoUtils;
import cn.iclass.vodfileupload.domian.VideoFile;
import cn.iclass.vodfileupload.repository.VideoFileRepository;
import com.aliyuncs.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/run")
public class TestController {
    @Autowired
    private VideoFileRepository repository;

    @GetMapping
    public void job() {
        File folder = new File("/Volumes/My Passport/国学系列/国学系列/钱锦国-《论语》的君子之道/");
        List<File> fileList = UploadVideoUtils.getFiles(folder);
        for (File file : fileList) {
            String title = file.getPath().replace("/Volumes/My Passport/国学系列/", "");
            String videoId = UploadVideoUtils.testUploadVideo(title, file.getPath());
            if (!StringUtils.isEmpty(videoId)) {
                VideoFile videoFile = new VideoFile();
                videoFile.setVideoId(videoId);
                videoFile.setPath(title);
                repository.save(videoFile);
                FileUtil.move(file, new File(file.getParent().replace("/Volumes/My Passport/",
                        "/Volumes/My Passport/已完成")), true);
            }

        }
    }
}
